package org.dtrd.modules.evaluation.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.PsychologyInfo;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdPsychologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_ADD;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * @Author QR
 * @Date 2021/11/1 2:00 PM
 */
@Api(tags = "量表评估")
@RestController
public class PsychologyController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdPsychologyService psychologyService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加心理健康临床症状评估")
    @PostMapping(value = {"/105/46/1054601", "/100/17/1001716"})
    public Result<?> savePsychologyInfo(@RequestBody PsychologyInfo psychologyInfo){
        if (psychologyInfo.getInput() == null || psychologyInfo.getInput().isEmpty()) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean inValidInput = psychologyInfo.getInput().size() != Evaluation.ProblemNum.PSYCHOLOGY.getNum();
        if (inValidInput) {
            return error(SystemState.ResponseState.STATE_PARAMS_ERROR);
        }
        Optional<Integer> any = psychologyInfo.getInput().stream()
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()) {
            return error(SystemState.ResponseState.STATE_PARAMS_ERROR);
        }
        PsychologyInfo info = psychologyService.savePsychologyInfo(psychologyInfo);
        String logContent = String.format("添加心理健康临床症状评估，添加用户id：%d", psychologyInfo.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询心理健康临床症状评估")
    @GetMapping("/105/46/1054602")
    public Result<?> getPsychologyInfoLatest(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        PsychologyInfo psychologyInfo = psychologyService.getPsychologyInfoLatest(patientId);
        return Result.OK(psychologyInfo);
    }
}
